use "build/output/main_panel.dta", clear
tsset coin_id date
keep if coin == "USDT"

format date %tdm-d

tempfile tf1
tempfile tf2
tempfile tf3
tempfile tf4 

gen market_cap_billions = market_cap / 10^9

label var iv_btc "BTC Implied Volatility"
label var funding_apr "Perpetual Futures Funding Premia"
label var market_cap_billions "Market Cap"
label var margin_aprFTX "FTX"
label var margin_aprBitfinex "Exchange 2"
label var date ""
label var p "Price"

format p %10.3fc

tsline p if date>=mdy(5,1,2022) & date<=mdy(5,31,2022) & coin == "USDT", subtitle("(b) Tether Price",  orientation(horizontal)  bexpand justification(left)) plotregion(margin(medium)) xline(22777, lcolor(gs10)) xtitle("") ytitle("") yline(1, lcolor(black)) saving(`tf1'.gph, replace)

tsline iv_btc funding_apr if date>=mdy(5,1,2022) & date<=mdy(5,31,2022), xline(22777, lcolor(gs10)) subtitle("(a) Index/Percent",  orientation(horizontal)  bexpand justification(left)) plotregion(margin(medium)) legend(ring(0) position(4) rows(2) size(vsmall) symysize(1)) saving(`tf2'.gph, replace) xtitle("") ytitle("") ysc(range(-150 150))  ylabel(-150(50)150)

tsline market_cap_billions if (date>=mdy(5,1,2022) & date<=mdy(5,31,2022) & coin == "USDT"), xline(22777, lcolor(gs10)) subtitle("(c) Tether Market Cap., $ Billions",  orientation(horizontal)  bexpand justification(left)) plotregion(margin(medium)) legend(ring(0) position(4) rows(2)) saving(`tf3'.gph, replace) xtitle("") ytitle("") 

tsline margin_aprFTX margin_aprBitfinex if date>=mdy(5,1,2022) & date<=mdy(5,31,2022) & coin == "USDT", xline(22777, lcolor(gs10)) subtitle("(d) Tether Margin Lending Rate, Percent",  orientation(horizontal)  bexpand justification(left)) plotregion(margin(medium)) legend(ring(0) position(2) rows(2)) saving(`tf4'.gph, replace) xtitle("") ytitle("")

gr combine `tf2'.gph `tf1'.gph `tf3'.gph `tf4'.gph, rows(2) xcommon scale(*1.15)
gr export "analysis/output_figures/figure_usdt_may_2022.pdf", replace   
